//ESPAÑOL
'use strict';
angular.module("ngLocale", [], ["$provide", function($provide) {
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
$provide.value("$locale", {
  "DATETIME_FORMATS": {
    "AMPMS": [
      "a. m.",
      "p. m."
    ],
    "DAY": [
      "domingo",
      "lunes",
      "martes",
      "mi\u00e9rcoles",
      "jueves",
      "viernes",
      "s\u00e1bado"
    ],
    "MONTH": [
      "enero",
      "febrero",
      "marzo",
      "abril",
      "mayo",
      "junio",
      "julio",
      "agosto",
      "septiembre",
      "octubre",
      "noviembre",
      "diciembre"
    ],
    "SHORTDAY": [
      "dom.",
      "lun.",
      "mar.",
      "mi\u00e9.",
      "jue.",
      "vie.",
      "s\u00e1b."
    ],
    "SHORTMONTH": [
      "ene.",
      "feb.",
      "mar.",
      "abr.",
      "may.",
      "jun.",
      "jul.",
      "ago.",
      "sept.",
      "oct.",
      "nov.",
      "dic."
    ],
    "fullDate": "EEEE, d 'de' MMMM 'de' y",
    "longDate": "d 'de' MMMM 'de' y",
    "medium": "d/M/y H:mm:ss",
    "mediumDate": "d/M/y",
    "mediumTime": "H:mm:ss",
    "short": "d/M/yy H:mm",
    "shortDate": "d/M/yy",
    "shortTime": "H:mm"
  },
  "NUMBER_FORMATS": {
    "CURRENCY_SYM": "$",
    "DECIMAL_SEP": ",",
    "GROUP_SEP": ".",
    "PATTERNS": [
      {
        "gSize": 3,
        "lgSize": 3,
        "maxFrac": 3,
        "minFrac": 0,
        "minInt": 1,
        "negPre": "-",
        "negSuf": "",
        "posPre": "",
        "posSuf": ""
      },
      {
        "gSize": 3,
        "lgSize": 3,
        "maxFrac": 2,
        "minFrac": 2,
        "minInt": 1,
        "negPre": "-",
        "negSuf": "\u00a0\u00a4",
        "posPre": "",
        "posSuf": "\u00a0\u00a4"
      }
    ]
  },
  "id": "es-ar",
  "pluralCat": function (n, opt_precision) {  if (n == 1) {    return PLURAL_CATEGORY.ONE;  }  return PLURAL_CATEGORY.OTHER;}
});
}]);


//MODULO PRINCIPAL
var agendana = angular.module('agendana', ['ui.bootstrap']);

//CONTROLADORES
//Controlador de alertas emergentes
agendana.controller('AlertasCtrl',['$rootScope',function($rootScope) {
    $rootScope.alertas=null;
}]);

//FACTORIES
agendana.factory('ajaxAuthInterceptor', function() {  
    var ajaxAuthInterceptor = {
            responseError: function(rejection) {
                if(rejection.status === 403){
                    window.location.reload();
                }
                return rejection;}
    };

    return ajaxAuthInterceptor;
});

agendana.factory('ajaxManager', ['$http','$rootScope', function($http,$rootScope) {
    return {
        update_scope_model: 
            function($scope,retrieved_data){
                for (var key in retrieved_data) {
                    $scope[key] = retrieved_data[key];
                }
            },
        ajax_get: 
            function($scope,url,config, sucess_callback, fail_callback){
            return $http.get(url.url,config)
            .success(angular.bind(this,function(retrieved_data){
                if(angular.isDefined(retrieved_data)){
                    this.update_scope_model($scope,retrieved_data.data);
                }
                
                if(retrieved_data.success){
                    if(angular.isDefined(sucess_callback)){
                        sucess_callback(retrieved_data.data);
                    }
                }else{
                    //mostrar errores
                    $scope.form.errores = retrieved_data.errores;
                    
                    if(angular.isDefined(fail_callback)){
                        fail_callback(retrieved_data);
                    }else if(!angular.isDefined(retrieved_data.alertas)){//por si no hay otra alerta defindia 
                        $rootScope.alertas=[{tipo:'danger',mensaje:'Error Fatal recargue la pagina y vuelva a intentarlo'}];
                    }
                }
                    
                
                if(angular.isDefined(retrieved_data.alertas)){
                    $rootScope.alertas = retrieved_data.alertas;
                }
            }))
            .error(function() {
                $scope.waiting_post_response = false;
                $rootScope.alertas=[{tipo:'danger',mensaje:'Error Fatal recargue la pagina y vuelva a intentarlo'}];
            });
        },
        ajax_post: 
            function($scope,url,data, sucess_callback, fail_callback){
                
                $scope.form.errores = null;
                if(angular.isDefined(data)){
                    data.csrf = url.csrf;
                }else{
                    data = {csrf: url.csrf};
                }
                
                return $http.post(url.url,data)
                .success(angular.bind(this,function(retrieved_data) {
                    if(angular.isDefined(retrieved_data.data)){
                        this.update_scope_model($scope,retrieved_data.data);
                    }
                    
                    if(angular.isDefined(retrieved_data.alertas)){
                        $rootScope.alertas = retrieved_data.alertas;
                    }
                    
                    if(retrieved_data.success){
                        if(angular.isDefined(sucess_callback)){
                            sucess_callback(retrieved_data);
                        }
                    }else{
                        //mostrar errores
                        $scope.form.errores = retrieved_data.errores;
                        
                        if(angular.isDefined(fail_callback)){
                            fail_callback(retrieved_data);
                        }else if(!angular.isDefined(retrieved_data.alertas)){//por si no hay otra alerta defindia 
                            $rootScope.alertas=[{tipo:'danger',mensaje:'Error Fatal recargue la pagina y vuelva a intentarlo'}];
                        }
                    }
                    
                }))
                .error(function() {
                        $rootScope.alertas=[{tipo:'danger',mensaje:'Error Fatal recargue la pagina y vuelva a intentarlo'}];
                    });
                }
    };
}]);
//SERIVCES

//DIRECTIVES
agendana.directive('validar', function() {
    return {
        restrict: 'A',
        require:  '^form',
        link: function (scope, el, attrs, formCtrl) {
          // find the text box element, which has the 'name' attribute
          var inputEl   = el[0].querySelector("[name]");
          // convert the native text box element to an angular element
          var inputNgEl = angular.element(inputEl);
          // get the name on the text box so we know the property to check
          // on the form controller
          var inputName = inputNgEl.attr('name');

          // only apply the has-error class after the user leaves the text box
          inputNgEl.bind('blur', function() {
            el.toggleClass('has-error', formCtrl[inputName].$invalid);
          });
        }
      };
    });
//FILTERS

//CONFIG
agendana.config(['$httpProvider','datepickerConfig','datepickerPopupConfig', function($httpProvider,datepickerConfig,datepickerPopupConfig) {  
    $httpProvider.interceptors.push('ajaxAuthInterceptor');
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    
    datepickerConfig.startingDay = 1;
    datepickerConfig.showWeeks = false;
    datepickerPopupConfig.showButtonBar=false;

}]);